package per.sunmes.cl.g.gsys;

import java.io.BufferedReader;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import per.sunmes.cl.g.data.CardData;
import per.sunmes.cl.g.data.CardEffect;
import per.sunmes.cl.g.data.cards.CardEffect1;
import per.sunmes.cl.g.data.cards.CardEffect2;
import per.sunmes.cl.g.data.cards.CardEffect3;
import per.sunmes.cl.g.data.cards.CardEffect4;
import per.sunmes.cl.g.data.cards.CardEffect5;
import per.sunmes.cl.g.data.cards.CardEffect6;
import per.sunmes.cl.g.data.cards.CardEffect7;
import per.sunmes.cl.g.data.cards.CardEffect8;
import per.sunmes.cl.g.sys.StringU;
import per.sunmes.cl.g.sys.U;

import com.badlogic.gdx.files.FileHandle;

public class CardFactory {

	private static Map<Integer, CardData> cardPools = new HashMap<Integer, CardData>();
	private static Map<Integer, CardEffect> cardEffects = new HashMap<Integer, CardEffect>();
	private static CardEffect defaultCardEffect;

	public static CardData getCardCopy(int id) {
		CardData src = cardPools.get(id);
		if (src == null) {
			return null;
		}
		CardData cd = new CardData();
		cd.setId(src.getId());
		cd.setName(src.getName());
		cd.setDescription(src.getDescription());
		cd.setPicPath(src.getPicPath());
		cd.setEffect(src.getEffect());
		return cd;
	}

	public static void loadDatas() {
		cardEffects.clear();
		initCardEffects();

		cardPools.clear();

		FileHandle dataFile = U.getFile("card/cards.txt");
		BufferedReader br = null;
		try {
			br = dataFile.reader(1024);

			String line;
			while ((line = br.readLine()) != null) {
				parseLine(line);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (br != null) {
				try {
					br.close();
				} catch (IOException e) {
					e.printStackTrace();
				}
			}
		}
	}

	private static void initCardEffects() {
		defaultCardEffect = new CardEffect1();
		cardEffects.put(1, new CardEffect1());
		cardEffects.put(2, new CardEffect2());
		cardEffects.put(3, new CardEffect3());
		cardEffects.put(4, new CardEffect4());
		cardEffects.put(5, new CardEffect5());
		cardEffects.put(6, new CardEffect6());
		cardEffects.put(7, new CardEffect7());
		cardEffects.put(8, new CardEffect8());
	}

	private static void parseLine(String line) {
		if (line.isEmpty()) {
			return;
		}
		// id 名称 图片 文字说明
		String[] fields = line.split("\t");
		if (fields.length == 4) {
			parseCardFields(fields);
		}
	}

	private static void parseCardFields(String[] fields) {
		int id = StringU.parseInt(fields[0], 0);
		String name = fields[1];
		String pic = fields[2];
		String txt = fields[3];

		CardData data = new CardData();
		data.setId(id);
		data.setName(name);
		data.setPicPath(getPicPath(pic));
		data.setDescription(txt);
		cardPools.put(id, data);

		appendCardEffect(data);
	}

	private static String getPicPath(String pic) {
		return "card/cardPics/" + pic;
	}

	private static void appendCardEffect(CardData data) {
		CardEffect ce = cardEffects.get(data.getId());
		if (ce == null) {
			ce = defaultCardEffect;
		}
		data.setEffect(ce);
	}

}
